package xcmg.device.service.split;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.split.SplitRecordDO;
import xcmg.device.dao.mapper.split.SplitRecordMapper;
import xcmg.device.infra.DateUtil;
import xcmg.device.infra.ExportUtil;
import xcmg.device.service.vo.split.SplitRecordVO;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Service
public class SplitRecordService {

    @Autowired
    private SplitRecordMapper splitRecordMapper;

    @Autowired
    private HttpServletResponse response;

    public PageCommonVO list(SearchCommonVO<SplitRecordVO> condition) {
        PageCommonVO<SplitRecordVO> pageCommonVO = new PageCommonVO();
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<SplitRecordDO> doList = splitRecordMapper.list(condition.getFilters());
        List<SplitRecordVO> voList = new ArrayList<>();
        for (SplitRecordDO entity : doList) {
            SplitRecordVO model = new SplitRecordVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(voList);
        return pageCommonVO;
    }

    public void exportAll(SplitRecordVO condition)throws Exception{
        Integer count = splitRecordMapper.listCount(condition);
        List<SplitRecordDO> dataList = new ArrayList<>();
        int times = count/500+1;
        for(int i=0; i<times; i++){
            PageHelperPlus.startPage(i + 1, 500, "create_date desc");
            dataList.addAll(splitRecordMapper.list(condition));
        }
        invokeExport(dataList);
    }

    private void invokeExport(List<SplitRecordDO> doList)throws Exception{
        String title = "拆件记录数据导出";
        String[] rowsName = new String[]{"序号","拆件单号","原件条码","原件零件编码","原件零件名称","原件数量",
                "拆件条码","拆件零件编码", "拆件零件名称", "拆件数量", "操作人", "操作时间"};
        List<Object[]>  dataList = new ArrayList<>();
        Object[] objs;
        for (int i = 0; i < doList.size(); i++) {
            SplitRecordDO exportDO = doList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i+1;
            objs[1] = exportDO.getSplitOrder();
            objs[2] = exportDO.getSourceBarcode();
            objs[3] = exportDO.getSourceMaterialsNo();
            objs[4] = exportDO.getSourceMaterialsDes();
            objs[5] = exportDO.getSourceAmount();
            objs[6] = exportDO.getSplitBarcode();
            objs[7] = exportDO.getSplitMaterialsNo();
            objs[8] = exportDO.getSplitMaterialsDes();
            objs[9] = exportDO.getSplitAmount();
            objs[10] = exportDO.getCreateName();
            objs[11] = DateUtil.formatDateTime(exportDO.getCreateDate());
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }
}
